package com.controller.generic_controller;

import com.pojo.User;
import com.service.iter.generic_service.ILoginService;
import com.util.MD5Utils;
import com.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * .
 * 登录控制器
 *
 * @author admin
 * 2023/06/19
 */
@RestController
@RequestMapping("verity/login")
public class LoginController {
    /**
     * .
     * 登录服务
     */
    @Autowired
    private ILoginService loginService;

    /**
     * .
     * 去登录
     *
     * @param user    用户
     * @param request 请求
     * @return {@link Res}
     */
    @PostMapping
    public Res goLogin(@RequestBody final User user,
                       final HttpServletRequest request) {
        String password = user.getPassword();
        String saltMD5 = MD5Utils.getSaltMD5(password);
        user.setPassword(saltMD5);
        Res aNull = loginService.isNull(user);
        User data = (User) aNull.getData();
        HttpSession session = request.getSession();
        session.setAttribute("user", data);
        System.out.println(data);
        return aNull;
    }
}
